<?php
/**
 * Login_Data_Userdata
 * set user data
 * @author Adrian Stolarski
 */
class Login_Data_Setuserdata implements Login_Data_Mainable, Login_Data_Preparable, 
        Login_Model_Getable {
    
    /**
     *
     * @var type $database
     * database handler
     */
    public $database;
    /**
     *
     * @var type $database_query
     * database query handler
     */
    public $database_query;
    
    /**
     * constructor()
     */
    public function __construct() {
        $this->database = new Core_Driver_MysqlImproved;
    }
    
    /**
     * main()
     * insert data to user object table
     * 
     * @param array $variables
     */
    public function main(array $variables = null) {
       $variables['id'] = $this->get_user_data($variables['user_name']);
       foreach($variables as $key => $value){
           $variables[$key] = $this->database->escape($value);
       }
       $this->prepare_query($variables);
       $this->database->connect();
       $this->database->prepare($this->database_query);
       $this->database->query();
       $this->database->dataOperations();
       
    }
    
    /**
     * get_user_data()
     * get user id from database
     * 
     * @param type $value
     * @param type $key
     * @return type id
     */
    public function get_user_data($value, $key = null) {
        $this->database->connect();
        $value = $this->database->escape($value);
        $this->database->prepare(
                "SELECT id from users_data WHERE user_name.users_data = '$value';"
                );
        $this->database->query();
        $user_data = $this->database->fetch('array');
        return $user_data['id'];
    }
    
    /**
     * prepare_query()
     * preapare database query
     * 
     * @param array $variables
     */
    public function prepare_query(array $variables = null) {
        $id = $variables['id'];
        $this->database_query = "insert into user_object(user_id, access_level_id, language_id) values($id, 2, 1);";
    }

}
